package com.meteor.downloader.videodownloader.clueft

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.graphics.drawable.IconCompat
import com.meteor.downloader.videodownloader.R
import com.meteor.downloader.videodownloader.StartMdmActivity
import com.meteor.downloader.videodownloader.mdmdata.DataDownloadingMdm
import com.meteor.downloader.videodownloader.utils.ApplicationMdmUtils
import com.meteor.downloader.videodownloader.utils.DataManagerMdmUtils
import com.meteor.downloader.videodownloader.utils.LogInitMdmUtils
import com.meteor.downloader.videodownloader.utils.MdmTotalUtils
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_NOTIFY_CHANNEL_ID
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_NOTIFY_CHANNEL_ID_TWO
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_NOTIFY_GRAND_TOTAL
import com.meteor.downloader.videodownloader.utils.value.ParamsMdmUtils.mCompleteVideoList
import java.util.Random

object MdmClueDownload {
    private var mCurSaverNotifyID = ""
    private var mCurSaverNotifyName = ""
    private var mNotifyId = 677

    fun initManagerAndDismiss(context: Context, cancelAll: Boolean): NotificationManager {
        val managerSaver =
            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (cancelAll) {
            try {
                managerSaver.cancelAll()
            } catch (_: Exception) {
            }
        }
        return managerSaver
    }

    private fun initChannelID(notificationManager: NotificationManager, saverPop: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (saverPop) {
                mCurSaverNotifyID = MDM_NOTIFY_CHANNEL_ID
                mCurSaverNotifyName = MDM_NOTIFY_CHANNEL_ID
                notificationManager.createNotificationChannel(
                    NotificationChannel(
                        mCurSaverNotifyID, mCurSaverNotifyName, NotificationManager.IMPORTANCE_HIGH
                    ).apply {
                        setSound(null, null)
                        setBypassDnd(true)
                        lockscreenVisibility = 1
                        enableVibration(false)
                    })
            } else {
                mCurSaverNotifyID = MDM_NOTIFY_CHANNEL_ID_TWO
                mCurSaverNotifyName = MDM_NOTIFY_CHANNEL_ID_TWO
                notificationManager.createNotificationChannel(
                    NotificationChannel(
                        mCurSaverNotifyID,
                        mCurSaverNotifyName,
                        NotificationManager.IMPORTANCE_DEFAULT
                    ).apply {
                        setSound(null, null)
                        setBypassDnd(true)
                        lockscreenVisibility = 1
                        enableVibration(false)
                    })
            }
        }
    }

    fun getTIClueShow(context: Context, saverPop: Boolean, from: String) {
        val grand = DataManagerMdmUtils.getDataKeyMdm(MDM_NOTIFY_GRAND_TOTAL, 0)
        val managerSaver = initManagerAndDismiss(context, false)
        initChannelID(managerSaver, saverPop)
        val remoteViews = RemoteViews(context.packageName, R.layout.layout_clue_mdm)
        val tiTexts = context.resources.getStringArray(R.array.mdm_notify_values)
        remoteViews.setTextViewText(R.id.tvContentImg, tiTexts[grand % tiTexts.size])
        ApplicationMdmUtils.getMeteorVideoSome()

        val index = grand % if (ApplicationMdmUtils.mVideoMdmList.isNotEmpty()) {
            ApplicationMdmUtils.mVideoMdmList.size
        } else {
            42
        }
        val bitmap = MdmTotalUtils.getClueForBitmap(context, index + 1)
        remoteViews.setImageViewBitmap(R.id.ivThumbImg, bitmap)
        val tiBuilder = NotificationCompat.Builder(
            context, if (saverPop) {
                mCurSaverNotifyID
            } else {
                mCurSaverNotifyName
            }
        ).setSmallIcon(IconCompat.createWithResource(context, R.drawable.mdm_til_clude_icon))
            .setCustomContentView(remoteViews).setPriority(
                if (saverPop) {
                    NotificationCompat.PRIORITY_HIGH
                } else {
                    NotificationCompat.PRIORITY_DEFAULT
                }
            ).setCustomBigContentView(remoteViews).setCustomHeadsUpContentView(remoteViews)
            .setCategory(NotificationCompat.CATEGORY_ALARM).setOnlyAlertOnce(false)
            .setStyle(NotificationCompat.BigPictureStyle()).setOngoing(false)
            .setContentIntent(contentPending(context, grand))
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setAutoCancel(true).setDefaults(-1)
            .setGroupSummary(false).build()
        try {
            if (mNotifyId > 678) {
                mNotifyId = 677
            }
            tiBuilder.flags = Notification.FLAG_AUTO_CANCEL
            managerSaver.notify(mNotifyId++, tiBuilder)

            DataManagerMdmUtils.setDataKeyMdm(MDM_NOTIFY_GRAND_TOTAL, grand + 1)
            LogInitMdmUtils.clueMdmValue(context, "${from}_create")
        } catch (e: Exception) {

        }
    }

    private fun contentPending(context: Context, grand: Int): PendingIntent {
        val intent = Intent(context, StartMdmActivity::class.java).apply {
            putExtra("mdmNotifyType", "true")
            putExtra("mdmNotifyGrandTotal", grand)
        }
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            PendingIntent.getActivity(
                context, Random().nextInt(4567), intent, PendingIntent.FLAG_IMMUTABLE
            )
        } else {
            PendingIntent.getActivity(
                context, Random().nextInt(4567), intent, PendingIntent.FLAG_IMMUTABLE
            )
        }
    }


    fun getMdmClueShowTwo(context: Context, saverPop: Boolean, data: DataDownloadingMdm) {
        val manager = initManagerAndDismiss(context, false)
        initChannelID(manager, saverPop)
        val remoteViews = RemoteViews(context.packageName, R.layout.layout_clue_mdm_b)
        remoteViews.setTextViewText(
            R.id.tvContentImg, context.getString(R.string.mdm_download_complete)
        )
        remoteViews.setImageViewBitmap(R.id.ivThumbImg, data.videoNotifyBitmapMdm)

        val tiBuilder = NotificationCompat.Builder(
            context, if (saverPop) {
                mCurSaverNotifyID
            } else {
                mCurSaverNotifyName
            }
        ).setSmallIcon(IconCompat.createWithResource(context, R.drawable.mdm_til_clude_icon))
            .setCustomContentView(remoteViews).setPriority(
                if (saverPop) {
                    NotificationCompat.PRIORITY_HIGH
                } else {
                    NotificationCompat.PRIORITY_DEFAULT
                }
            ).setCustomBigContentView(remoteViews).setCustomHeadsUpContentView(remoteViews)
            .setCategory(NotificationCompat.CATEGORY_ALARM).setOnlyAlertOnce(false)
            .setStyle(NotificationCompat.BigPictureStyle()).setOngoing(false)
            .setContentIntent(contentPendingTwo(context, data))
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setAutoCancel(true).setDefaults(-1)
            .setGroupSummary(false).build()
        try {
            tiBuilder.flags = Notification.FLAG_AUTO_CANCEL
            manager.notify(95, tiBuilder)

            LogInitMdmUtils.clueCompleteValue(context, "create")
        } catch (_: Exception) {
        }
    }

    private fun contentPendingTwo(
        context: Context,
        data: DataDownloadingMdm
    ): PendingIntent {
        mCompleteVideoList.add(data)
        val intent = Intent(context, StartMdmActivity::class.java).apply {
            putExtra("clueLink", data.videoUrlMdm)
        }
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            PendingIntent.getActivity(
                context, Random().nextInt(14444), intent, PendingIntent.FLAG_IMMUTABLE
            )
        } else {
            PendingIntent.getActivity(
                context, Random().nextInt(18888), intent, PendingIntent.FLAG_IMMUTABLE
            )
        }
    }
}